angular.module('GiveStock')

.controller('GiveStock.HomeCtrl', ['$scope', '$state', 'GiveStockHomeService', 'User', 'imanDialog', '$ionicLoading', function($scope, $state, GiveStockHomeService, User, imanDialog, $ionicLoading) {
    $scope.data = {};
    var barcodeMasters = [];
    var listData = [];
    $scope.connectAreaName = "";

    $scope.data.gridOptions = {
        enableRefresh: true,

        columnDefs: [{
            col: 'Priority',
            name: '优先级',
            cls: 'givestock-column-underline'
        }, {
            col: 'NOLotNumber',
            name: '批次'
        }, {
            col: 'ItemMasterCode',
            name: '料号'
        }, {
            col: 'ItemMasterName',
            name: '品名'
        }, {
            col: 'TotalDemandQty',
            name: '下游要货'
        }, {
            col: 'TotalNeedQty',
            name: '待备货量',
            cls: 'givestock-column-red'
        }, {
            col: 'RcvQty',
            name: '超市库存',
            cls: 'givestock-column-green'
        }, {
            col: 'TotalStockQty',
            name: '已备货量'
        }, {
            col: 'TotalReceiveQty',
            name: '接收数量'
        }],
        data: []
    };

    function initData() {
        $ionicLoading.show({
            template: 'Loading...'
        });
        GiveStockHomeService.getConnectArea("KB0000001").then(function(data) {
            $scope.connectAreaName = data.Name;

            GiveStockHomeService.getGiveStockData(User.get("_id")).then(function(data) {
                $ionicLoading.hide();
                if (data == null || data.length === 0) {
                    imanDialog.alert('获取看板数据为空', '');
                } else {
                    barcodeMasters = data;
                    for (var i = 1; i <= 20; i++) {
                        barcodeMasters.push({
                            Priority: 50 - i,
                            NOLotNumber: 'P2012',
                            ItemMasterCode: 'ST021',
                            ItemMasterName: '网拍',
                            TotalDemandQty: 200,
                            TotalNeedQty: 100,
                            RcvQty: 300,
                            TotalStockQty: 100,
                            TotalReceiveQty: 0
                        });
                    }
                    $scope.data.gridOptions.data = barcodeMasters;
                    listData = $scope.data.gridOptions.data;
                }


            }, function(error) {
                $ionicLoading.hide();
                imanDialog.alert('获取看板数据失败', '');
                for (var i = 1; i <= 20; i++) {
                    barcodeMasters.push({
                        Priority: 50 - i,
                        NOLotNumber: 'P2012',
                        ItemMasterCode: 'ST021',
                        ItemMasterName: '网拍',
                        TotalDemandQty: 200,
                        TotalNeedQty: 100,
                        RcvQty: 300,
                        TotalStockQty: 100,
                        TotalReceiveQty: 0
                    });
                }
                $scope.data.gridOptions.data = barcodeMasters;
                listData = $scope.data.gridOptions.data;
            });

        }, function(error) {
            $ionicLoading.hide();
            imanDialog.alert('获取交接区失败', '');

        });

    }
    initData();



    //对批次物料条件模糊查询
    $scope.search = function(query) {

        if (query === "") {
            $scope.data.gridOptions.data = listData;
        }
        if (query !== "") {
            var nPosMo;
            var nPosItem;
            $scope.data.gridOptions.data = [];

            for (var i in listData) {
                var noLotNumber = listData[i].NOLotNumber;
                var itemMasterCode = listData[i].ItemMasterCode;
                nPosMo = find(query, noLotNumber);
                nPosItem = find(query, itemMasterCode);
                //nPos=sTxt.indexOf(sFind);   
                if (nPosMo >= 0 || nPosItem >= 0) {

                    $scope.data.gridOptions.data.push(listData[i]);
                }
            }
            if ($scope.data.gridOptions.data.length === 0) {
                imanDialog.alert('未找到相关信息', '');
            }
        }
    };

    function find(sFind, sObj) {
        var nSize = sFind.length;
        var nLen = sObj.length;
        var sCompare;

        if (nSize <= nLen) {
            for (var i = 0; i <= nLen - nSize + 1; i++) {
                sCompare = sObj.substring(i, i + nSize);
                if (sCompare === sFind) {
                    return i;
                }
            }
        }
        return -1;
    }

    $scope.doRefresh = function(operate, item) {
        $scope.data.gridOptions.data.unshift({
            Priority: 100,
            NOLotNumber: 'P2016',
            ItemMasterCode: 'SA003',
            ItemMasterName: '新网拍',
            TotalDemandQty: 200,
            TotalNeedQty: 100,
            RcvQty: 300,
            TotalStockQty: 100,
            TotalReceiveQty: 0
        });
        $scope.$broadcast('scroll.refreshComplete');
    };
}]);